package com.anpy.album.service.impl;

import com.anpy.album.entity.AlbumStatisticsEntity;
import com.anpy.album.entity.PhotoEntity;
import com.anpy.album.mapper.PhotoMapper;
import com.anpy.album.service.OssFeign;
import com.anpy.album.service.PhotoService;
import com.anpy.common.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service("photoServiceImpl")
public class PhotoServiceImpl extends BaseServiceImpl<PhotoMapper, PhotoEntity> implements PhotoService {

    @Resource
    private PhotoMapper photoMapper;

    @Resource
    private OssFeign ossFeign;

    @Override
    public boolean savePhoto(PhotoEntity vo) {
        vo.setPhotoUrl("https://apy-test.oss-cn-beijing.aliyuncs.com/" + vo.getPhotoName());
        boolean save = save(vo);

        if (save) {
            AlbumStatisticsEntity entity = new AlbumStatisticsEntity();
            entity.setUserId(vo.getUserId());
            entity.setAlbumId(vo.getAlbumId());
            Integer statisticsAlbumId = photoMapper.queryStatisticsAlbum(entity);
            if (statisticsAlbumId == null) {
                photoMapper.saveStatisticsAlbum(entity);
            } else {
                photoMapper.updateStatisticsAlbum(entity);
            }
        }
        return save;
    }


    @Override
    public List<Map<String, Object>> list(Integer albumId) {
        LambdaQueryWrapper<PhotoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PhotoEntity::getAlbumId, albumId);
        wrapper.orderByDesc(PhotoEntity::getCreateAt);
        List<PhotoEntity> list = list(wrapper);
        //LinkedHashMap::new, Collectors.toList() 解决分组排序问题
        Map<Date, List<PhotoEntity>> map = list.stream().collect(Collectors.groupingBy(PhotoEntity::getCreateAt, LinkedHashMap::new, Collectors.toList()));
        List<Map<String, Object>> listResponse = new ArrayList<>();
        for (Map.Entry<Date, List<PhotoEntity>> entry : map.entrySet()) {
            Map<String, Object> mapResponse = new HashMap<>();
            Date key = entry.getKey();
            List<PhotoEntity> value = entry.getValue();
            mapResponse.put("date", key);
            mapResponse.put("list", value);
            listResponse.add(mapResponse);
        }
        return listResponse;
    }

    @Override
    public boolean removePhoto(List<Integer> photoIds) {
        // 暂时不从 OSS 中删除照片文件
        // ArrayList<String> photoNames = photoMapper.queryPhotoNames(photoIds);
        // ossFeign.removePhoto(photoNames);
        return photoMapper.deleteByPhotoIds(photoIds);
    }
    
    @Override
    public boolean removePhotoByUserIdAndPhotoIds(Integer userId, List<Integer> photoIds) {
        if (userId == null || photoIds == null || photoIds.isEmpty()) {
            return false;
        }
        // 暂时不从 OSS 中删除照片文件
        // ArrayList<String> photoNames = photoMapper.queryPhotoNamesByUserIdAndPhotoIds(userId, photoIds);
        // if (photoNames != null && !photoNames.isEmpty()) {
        //     ossFeign.removePhoto(photoNames);
        // }
        // 在数据库中逻辑删除照片记录（更新 deleted_at 字段）
        return photoMapper.deleteByUserIdAndPhotoIds(userId, photoIds);
    }
}
